package com.b2.service.impl;

import com.b2.constant.MessageConstant;
import com.b2.mapper.AbilityMapper;
import com.b2.mapper.BookCategoryMapper;
import com.b2.mapper.BookMapper;
import com.b2.pojo.dto.BookDto;
import com.b2.pojo.entity.Ability;
import com.b2.pojo.entity.Book;
import com.b2.result.Result;
import com.b2.service.BookService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author 悦神
* @description 针对表【book】的数据库操作Service实现
* @createDate 2024-05-27 10:28:35
*/
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
    implements BookService{
    @Autowired
    BookCategoryMapper bookCategoryMapper;

    @Autowired
    BookMapper bookMapper;

    @Autowired
    AbilityMapper abilityMapper;


    @Override
    public Result<List<Book>> check(Long id) {
        LambdaQueryWrapper<Book> bookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookLambdaQueryWrapper.eq(Book::getBookCategoryId,id);
        List<Book> books = bookMapper.selectList(bookLambdaQueryWrapper);
        return Result.success(books);
    }
    @Transactional
    @Override
    public Result delete(Long id) {

        LambdaQueryWrapper<Ability> abilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        abilityLambdaQueryWrapper.eq(Ability::getBookId,id);
        List<Ability> abilities = abilityMapper.selectList(abilityLambdaQueryWrapper);
        if (abilities.size()>0) {
            throw new RuntimeException(MessageConstant.ABILITY_DELETE_FAIL);
        }
        bookMapper.deleteById(id);
        return Result.success();
    }

    @Override
    @Transactional
    public Result add(BookDto bookDto) {
        String regex6 = "^[A-Z]{4}[0-9]{2}$"; // 长度位6，第5.6为数字

      //判断绘本编码是否重复
        LambdaQueryWrapper<Book> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(Book::getBookCode,bookDto.getBookCode());
        List<Book> books = bookMapper.selectList(objectLambdaQueryWrapper);
        if (books.size()>0) {
            throw new RuntimeException(MessageConstant.BOOK_CODE_EXIST);
        }

       //判断绘本名字是否重复
        LambdaQueryWrapper<Book> objectLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper1.eq(Book::getBookName,bookDto.getBookName());
        List<Book> books1 = bookMapper.selectList(objectLambdaQueryWrapper1);
        if (books1.size()>0) {
            throw new RuntimeException(MessageConstant.BOOK_NAME_EXIST);
        }

        //判断绘本编码是否符合要求
        Pattern compile = Pattern.compile(regex6);
        Matcher matcher = compile.matcher(bookDto.getBookCode());
        if (!matcher.matches()) {
            throw new RuntimeException(MessageConstant.BOOK_CODE_FAIL);
        }
        Book book = new Book();
        book.setBookName(bookDto.getBookName());
        book.setBookCode(bookDto.getBookCode());
        book.setBookCategoryId(bookDto.getBookCategoryId());
        bookMapper.insert(book);
        return Result.success();
    }
}




